package fitness;

import java.util.ArrayList;

import chromo.Chromosome;

public class FitnessCalc {

	private static ArrayList<ArrayList<Boolean>> output = new ArrayList<ArrayList<Boolean>>();
	
	/**
	 * Gets the solution.
	 *
	 * @return the solution
	 */
	//A method to return the outputs of a system
	public static ArrayList<ArrayList<Boolean>> getSolution(){
		return output;
	}
	//This detects what fitness we are looking for by detecting how many outputs there are
	public static int maxFitness(){
		return output.size();
	}
	
	/**
	 * Sets the solution. Must add them one at a time
	 *
	 * @param finalOutput the new solution
	 */
	public static void setSolution(ArrayList<Boolean> finalOutput){
		output.add(new ArrayList<Boolean>(finalOutput));
	}
	//This is the one of the most important methods that will take all the outputs of a chromosome and see how many match the solution
	public static int getFitness(Chromosome chromo){
		ArrayList<ArrayList<Boolean>> answer = chromo.getAllOutputs();
		int fitness = 0;
		for(ArrayList<Boolean> desiredAnswer : output){
			for(ArrayList<Boolean> chromoAnswer : answer){
				int matcher = 0;
				for(int i = 0; i < desiredAnswer.size(); i++){
					try{
					if(desiredAnswer.get(i) == chromoAnswer.get(i))
						matcher++;
					}
					catch(IndexOutOfBoundsException e){
						System.out.println(e.getMessage());
					}
					
				}
				if(matcher == desiredAnswer.size()){
					fitness++;
					
					if(fitness == FitnessCalc.maxFitness()){
						System.out.println("break");
					}
					break;
				}
				matcher = 0;
			}
			
		}
		//TODO Don't think this is needed
//		chromo.setFitness(fitness);
		return fitness;
		
	}
}
